
var sel_ajax = (function(settings){
    settings = $.extend({'use_choose_btn':true,'max_choosed_num':3},settings);

    // console.log(settings);

    var     choosed_num = 0,
            max_choosed_num = settings.max_choosed_num,  //最大选择数
            selected_class     = 'back999',  //选中的样式back999
            not_selected_class = 'back87C743';  //未选中的样式
            recmd_job_url = "<?= \yii\helpers\Url::to(['/owner/default/get_coll_job'])?>";

       //添加和删除的操作函数  用于下方的收藏职位的选择和取消的操作
        function operate($type,obj)
        {
            var $li = obj.closest('li'),
                owner_id = obj.data('id'),
                owner_name = $li.find('.owner_name').html(),
                icon     = $li.find('.icon').attr('src'),
                job      =$li.find('.last_profession').html(),
                url = $li.find('a').eq(0).attr('href');
                html = '';
        
            if($type == 0){

                //取消
                $('#'+owner_id).closest('li').remove();
                $('.selected_num').html(choosed_num);
                
            }else if($type == 1){

                //选择
                $('.selected_num').html(choosed_num);

                html = ' <li>'+
                            '<a href="##"><img width="62px" height="62px" class="fl" src="'+icon+'" alt=""></a>'+
                            '<div class="right-font">'+
                                '<p class="name">'+owner_name+'</p>'+
                                '<p>'+job+'</p>'+
                            '</div>'+
                            '<a class="remove" href="##" id="'+owner_id+'">移除</a>'+
                       '</li>';
                // console.log(html);
                $('.selected_job').prepend(html);
                
            }

            //判断是否要显示确认推荐按钮
            if(choosed_num > 0){
                $('.selected_job>a').show();
            }else{
                 $('.selected_job>a').hide();
            }
        }


         function operate_fix(type,obj)
         {    
           head_id = obj.data('id');
           if(type ==1){

              //选择
              $.each($('.choose_btn'),function(i){
                 if($(this).data('id') == head_id ){
                    $(this).removeClass(not_selected_class).addClass(selected_class).html('取消');
                 }
              })
           }else{
              //取消
              $.each($('.choose_btn'),function(i){
                 if($(this).data('id') == head_id ){
                    $(this).removeClass(selected_class).addClass(not_selected_class).html('选择');
                 }
              })
           }
        }

        //点击分页按钮进行ajax请求收藏职位数据
        $('body').delegate('.pagination a','click',function(event){
            var e = event || window.event,
                page = $(this).data('page')+1,
                $ul  = $(this).closest('ul.ajax_url'),
                ajax_url = $ul.data('ajax_url'),
                selected_job = [];

            if(e.preventDefault){
                e.preventDefault();
            }else{
                e.returnValue = false;
            }

            //得到所有已经选择的职位id
            $.each($('.selected_job .remove'),function(event){
                selected_job.push($(this).attr('id'));
            })
            // console.log(selected_job);
            //ajax调取数据
            $.ajax({
                type : 'GET',
                dataType:'html',
                async: true,
                url  : ajax_url,
                data : {'selected_head':selected_job,'page':page},
                success:function(data){
                    $ul.html(data);
                },
            })
        }).delegate('.choose_btn','click',function(event){
            var e = event || window.event;
             if(e.preventDefault){
                e.preventDefault();
            }else{
                e.returnValue = false;
            }

            if(!settings.use_choose_btn){
                location.href = $(this).attr('href');
                return true;
            }

            //查看该对象是否已经点击
            if($(this).hasClass(selected_class)){

                //点击取消
                 choosed_num-=1;
                 operate(0,$(this));
                 operate_fix(0,$(this));
                 
                 // console.log(choosed_num);
                
            }else{

                //点击选择
                if(choosed_num < max_choosed_num){
                    choosed_num+=1;
                    operate(1,$(this));
                    operate_fix(1,$(this));
                    // console.log(choosed_num);
                }else{
                    alert($('.alert_tips').html());
                }
               
            }
        }).delegate('.remove','click',function(event){
            var head_id = $(this).attr('id');

            if(!settings.use_choose_btn){
                return true;
            }

            //移除自身的职位
            $(this).closest('li').remove();
            choosed_num-=1;
            $('.selected_num').html(choosed_num);


             //判断是否要显示确认推荐按钮
            if(choosed_num > 0){

                $('.selected_job>a').show();
            }else{
                 $('.selected_job>a').hide();
            }

            //移除下方的收藏职位的选中样式
            $.each($('.choose_btn'),function(){
                if($(this).data('id') == head_id){
                    $(this).removeClass(selected_class).addClass(not_selected_class).html('选择');
                }
            })
        })

        $('.selected_job>a').click(function(event){
            var e = event || window.event,
                selected_job = [];
                // job_id = $(this).closest('div').find('input[name="job_id"]').val();
            if(e.preventDefault){
                e.preventDefault();
            }else{
                e.returnValue = false;
            }

            //得到所有已经选择的职位id
            $.each($('.selected_job .remove'),function(event){
                selected_job.push($(this).attr('id'));
            })
    
            location.href = $(this).attr('href')+'&owner_id='+selected_job.join('-');
        })
    })


